package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.DepartmentAddNewParam;
import cn.oms.manage.pojo.param.DepartmentUpdateParam;
import cn.oms.manage.pojo.vo.DepartmentListVO;
import cn.oms.manage.pojo.vo.DepartmentStandardVO;
import cn.oms.manage.service.IDepartmentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 说明：
 *
 * @author: Fcx
 * @date: 2023/6/19
 **/
@Slf4j
@RestController
@RequestMapping("/departments")
@Validated
@Api(tags = "04. 科室管理模块")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    public DepartmentController() {
        log.debug("创建控制器类对象：DepartmentController");
    }

    // http://localhost:9080/departments/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/departments/add-new')")
    @ApiOperation("添加科室")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid DepartmentAddNewParam departmentAddNewParam) {
        log.debug("开始处理【添加科室】的请求，参数：{}", departmentAddNewParam);
        departmentService.addNew(departmentAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9080/departments/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('departments/delete')")
    @ApiOperation("根据ID删除科室")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "科室ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的科室ID值！") Long id) {
        log.debug("开始处理【根据ID删除科室】的请求，参数：{}", id);
        departmentService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/departments/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/departments/update')")
    @ApiOperation("修改科室详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "科室ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的科室ID值！") Long id,
                                           @Valid DepartmentUpdateParam departmentUpdateInfoParam) {
        log.debug("开始处理【修改科室详情】的请求，ID：{}，新数据：{}", id, departmentUpdateInfoParam);
        departmentService.updateInfoById(id, departmentUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9080/departments/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/departments/read')")
    @ApiOperation("根据ID查询科室详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "科室ID", required = true, dataType = "long")
    })
    public JsonResult<DepartmentStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的科室ID值！") Long id) {
        log.debug("开始处理【根据ID查询科室详情】的请求，参数：{}", id);
        DepartmentStandardVO queryResult = departmentService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/departments
    @GetMapping("")
    @PreAuthorize("hasAuthority('/departments/read')")
    @ApiOperation("查询科室列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<DepartmentListVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                       String queryType) {
        log.debug("开始处理【查询科室列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<DepartmentListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = departmentService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = departmentService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
    
}
